package com.dingreading.cloud.admin.service.impl;

import com.dingreading.cloud.admin.entity.WhStockOutGoodsLog;
import com.dingreading.cloud.admin.entity.table.WhStockOutGoodsLogTableDef;
import com.dingreading.cloud.admin.mapper.WhStockOutGoodsLogMapper;
import com.dingreading.cloud.admin.service.WhStockOutGoodsLogService;
import com.dingreading.cloud.common.util.PageUtil;
import com.dingreading.cloud.common.util.QueryFiledUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2023-11-22
 */
@Service
public class WhStockOutGoodsLogServiceImpl extends BaseServiceImpl<WhStockOutGoodsLogMapper, WhStockOutGoodsLog, WhStockOutGoodsLogTableDef> implements WhStockOutGoodsLogService {

    @Override
    protected WhStockOutGoodsLogTableDef getTable() {
        return WhStockOutGoodsLogTableDef.whStockOutGoodsLog;
    }

    @Override
    public List<WhStockOutGoodsLog> getOutGoodsLog(String orderCode, String outCode, Long shopCartId, Long goodsTypeId) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.orderCode.eq(orderCode))
                .and(table.outCode.eq(outCode))
                .and(table.shopCartId.eq(shopCartId))
                .and(table.goodsTypeId.eq(goodsTypeId))
                .and(table.status.eq(1));

        return getMapper().selectListByCondition(condition);
    }

    @Override
    public boolean setRefund(List<Long> id, String staffUid, String staffName, Date time) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.id.in(id))
                .and(table.status.eq(1));

        return UpdateChain.of(WhStockOutGoodsLog.class)
                .set(table.status, -1)
                .set(table.statusName, "出库已退")
                .set(table.refundUid, staffUid)
                .set(table.refundName, staffName)
                .set(table.refundTime, time)
                .where(condition)
                .update();
    }

    @Override
    public Page<WhStockOutGoodsLog> pageList(PageUtil pageUtil, String keyword, Integer status, Integer stockOutMethod, String beginDate, String endDate) {
        QueryCondition condition = QueryCondition.createEmpty();
        if (StringUtils.isNotBlank(keyword)) {
            condition.and(table.orderCode.like(keyword)
                    .or(table.outCode.like(keyword))
                    .or(table.goodsTypeName.like(keyword))
                    .or(table.goodsName.like(keyword))
                    .or(table.purchaseCode.like(keyword))
                    .or(table.inCode.like(keyword))
            );
        }
        if (null != status)
            condition.and(table.status.eq(status));
        if (null != stockOutMethod)
            condition.and(table.stockOutMethod.eq(stockOutMethod));
        if (StringUtils.isNotBlank(beginDate) && StringUtils.isNotBlank(endDate)) {
            condition.and(table.addTime.between(QueryFiledUtil.appendMin00(beginDate), QueryFiledUtil.appendMin24(endDate)));
        }

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.id.desc());

        return getMapper().paginate(Page.of(pageUtil.getPage(), pageUtil.getRows()), wrapper);
    }

    @Override
    public List<WhStockOutGoodsLog> getExportList(String keyword, Integer status, Integer stockOutMethod, String beginDate, String endDate) {
        QueryCondition condition = QueryCondition.createEmpty();
        if (StringUtils.isNotBlank(keyword)) {
            condition.and(table.orderCode.like(keyword)
                    .or(table.outCode.like(keyword))
                    .or(table.goodsTypeName.like(keyword))
                    .or(table.goodsName.like(keyword))
                    .or(table.purchaseCode.like(keyword))
                    .or(table.inCode.like(keyword))
            );
        }
        if (null != status)
            condition.and(table.status.eq(status));
        if (null != stockOutMethod)
            condition.and(table.stockOutMethod.eq(stockOutMethod));
        if (StringUtils.isNotBlank(beginDate) && StringUtils.isNotBlank(endDate))
            condition.and(table.addTime.between(QueryFiledUtil.appendMin00(beginDate), QueryFiledUtil.appendMin24(endDate)));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.id.desc());

        return getMapper().selectListByQuery(wrapper);
    }
}
